<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/3/27
 * Time: 10:45
 */

namespace Admin\Controller;

use Think\Controller;

class DkmxController extends AdminController
{
    //对父类构造函数的补充
    public function _myinitialize()
    {
        $this->db = M('dkmx');
    }

    public function lst()
    {
        $session_where=I('session_where');//不为空，则表明是从add/edit/del中跳转过来的，刚查询条件从session中取。
        if(empty($session_where)){
            //查询条件
            $where['status'] = array('gt', 0);

            //所有供应商名称列表
            $gysmc_arr = $this->db->where($where)->getField('gysmc', true);
            $gysmc_arr = array_values(array_unique($gysmc_arr));
            $this->assign('gysmc_arr', $gysmc_arr);

            //所有对应内部公司列表
            $dynbgs_arr = $this->db->where($where)->getField('dynbgs', true);
            $dynbgs_arr = array_values(array_unique($dynbgs_arr));
            $this->assign('dynbgs_arr', $dynbgs_arr);

            $month = I('month');
            $year = I('year');
            if (!empty($month) && !empty($year)) { //所属月份
                $ssyf = $year . $month;
                $where['yf'] = $ssyf;
            }

            $gysmc = I('gysmc');
            $dynbgs = I('dynbgs');

            if (!empty($gysmc)) $where['gysmc'] = $gysmc;
            if (!empty($dynbgs)) $where['dynbgs'] = $dynbgs;

            if (count($where) === 1) { //此时查询条件仅为status=1，表示没有别的查询条件，则默认查询条件为：只显示上月的数据
                $last_m = date("Ym",strtotime(date("Ym01",time()))-8600);;//默认情况下显示上月的数据
                $where['yf'] = $last_m;
            }

            //将查询条件存入session中，方便更新时跳转
            session(CONTROLLER_NAME."where",$where);
        }else{
            $where=session(CONTROLLER_NAME."where");
        }

        $map['yf']=$where["yf"];
        $map['status'] = array('gt', 0);
        //列表数据
        $lsts = $this->db->where($where)->order("updatetime desc")->select();
        $ys1_sum = $this->db->where($map)->getField('sum(yf1)');//当月所有应付总计
        $yfwf_sum = $this->db->where($map)->getField('sum(yfwf)');//当月所有应付未付总计

        //计算出调整后应付占比和应付未付占比
        foreach ($lsts as $k => $v) {
            $lsts[$k]['yfzb'] = round($v['tzyf'] / $ys1_sum * 100, 2)."%";//调整后应付/当月全部客户应付总和
            $lsts[$k]['wfzb'] = round($v['yfwf'] / $yfwf_sum * 100, 2)."%";//应付未付/当月所有应付未付总计
        }
        $this->assign('lsts', $lsts);//列表数据
        $this->display();
    }

    //新增 www.glxt.com/admin/khzy/add
    public function add()
    {
        if (IS_POST) {
            $data = $this->db->create();
            if ($data === false) {
                $this->error($this->db->getError());
            } else {
                $data['updatetime'] = date('Ymd', time());
                $data['sssj'] = date('Ymd', strtotime($data['sssj']));
                $data['yf'] = date('Ym', strtotime($data['sssj']));
                $data['status'] = 1;
                $data['tzyf'] = $data['yf1'] + $data['tz'];//调整后应付
                $data['yfwf']=$data['tzyf']-$data['sf'];//应付未付=调整后应付-实付
            }
            $rid = $this->db->add($data);

            if ($rid === false) {
                $this->error($this->db->getDbError());
            }

            $this->redirect('lst',array("session_where"=>"session_where"));
        }
    }

    //修改
    public function edit()
    {
        if (IS_POST) {
            $data = $this->db->create();
            if ($data === false) {
                $this->error($this->db->getError());
            } else {
                $data['updatetime'] = date('Ymd', time());
                $data['sssj'] = date('Ymd', strtotime($data['sssj']));
                $data['yf'] = date('Ym', strtotime($data['sssj']));
                $data['status'] = 1;
                $data['tzyf'] = $data['yf1'] + $data['tz'];//调整后应付
                $data['yfwf']=$data['tzyf']-$data['sf'];//应付未付=调整后应付-实付
            }
            $rid = $this->db->save($data);

            if ($rid === false) {
                $this->error($this->db->getDbError());
            }

            $this->redirect('lst',array("session_where"=>"session_where"));
        } else { //回显
            $id = I('id');
            $data = $this->db->where(array('id' => $id))->find();
            $this->ajaxReturn($data);
        }
    }

    //删除不是物理删除，只是状态的改变
    public function del()
    {
        $id = I('id');
        if (!empty($id)) {
            $data['status'] = 0;
            $data['updatetime'] = time();
            $this->db->where(array('id' => $id))->setField($data);
            $this->redirect('lst',array("session_where"=>"session_where"));
        }
    }
}